//
//  DetailsViewController.m
//  A_HandHeldLOL
//
//  Created by Zhao YanBin on 16/1/19.
//  Copyright © 2016年 zhaoyanbin. All rights reserved.
//

#import "DetailsViewController.h"
#import "AFNModel.h"
#import "TeamModel.h"
#import "DetailsTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "SaishiModel.h"
#import "XiangguanTableViewCell.h"
#import "XiangguanModel.h"
#import "ZhanDuiViewController.h"
#import <MJRefresh.h>
#import "SaishiXQViewController.h"
#import "XiangguanXQViewController.h"
@interface DetailsViewController ()<UITableViewDataSource, UITableViewDelegate,UIScrollViewDelegate>
//赛事tableView
@property(nonatomic, retain)UITableView *tableView;
//tableView请求用的数组
@property(nonatomic, retain)NSMutableArray *sArr;
//相关tableView
@property(nonatomic, retain)UITableView *tableV;
@property(nonatomic, retain)NSMutableArray *xArr;
//头像轮播数组字典
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, retain)NSMutableDictionary *dic;

//头部
@property(nonatomic, retain)UIImageView *team;
@property(nonatomic, retain)UILabel *name;
@property(nonatomic, retain)UILabel *fans;
@property(nonatomic, retain)UIButton *zhanduiziliao;
@property(nonatomic, retain)UIButton *guanzhu;
@property(nonatomic, retain)UILabel *fensi;



//赛程 相关btn
//@property(nonatomic, retain)UIButton *competitionbtn;
//@property(nonatomic, retain)UIButton *concernbtn;

//分段控制器
@property(nonatomic, retain)UISegmentedControl *seg;

//头像轮播
@property(nonatomic, retain)UIScrollView *scrollview;

//赛事相关scrollview
@property(nonatomic, retain)UIScrollView *competitionVC;




@end

@implementation DetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //头部
    [self head];
    
    //头像轮播图
    [self createscroll];
    
    //数据请求
    [self data];
    
    //分段控制器
    [self segented];
    
    //相关赛事下方scrollview
    [self createcomtitionVC];
    
    //数据请求
    [self saishiqingqiu];
    [self xiangguan];
}




//分段控制器
-(void)segented
{
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"赛事",@"相关"]];
    _seg.frame = CGRectMake(30, 250, 315, 30);
    [self.view addSubview:_seg];
    [_seg release];
    
    //分段索引
    _seg.selectedSegmentIndex = 0;
    
    
    //添加点击事件
    //valueChange 值变化时 触发方法
    [_seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
}
-(void)segAction:(UISegmentedControl *)seg
{
    if (_seg.selectedSegmentIndex == 0) {
        CGPoint p = {seg.selectedSegmentIndex,0};
        [_competitionVC setContentOffset:p];
    }else if(_seg.selectedSegmentIndex == 1){
        
        CGPoint p = {seg.selectedSegmentIndex * 375,0};
        [_competitionVC setContentOffset:p];
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    if (scrollView == self.competitionVC) {
            if (self.competitionVC.contentOffset.x == 0) {
                    _seg.selectedSegmentIndex = 0;
                }else if(self.competitionVC.contentOffset.x == 375){
                    _seg.selectedSegmentIndex = 1;
                }
//            }
    
}


-(void)createcomtitionVC
{
    //创建
    self.competitionVC = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, self.view.frame.size.height - 300 - 49)];
        _competitionVC.backgroundColor = [UIColor redColor];
    //内容大小
    self.competitionVC.contentSize= CGSizeMake(self.view.frame.size.width *2, 0);
    //    [_scrollview release];
    
    //整页翻动
    _competitionVC.pagingEnabled = YES;
    self.competitionVC.delegate = self;
    [self.view addSubview:_competitionVC];

    
    //赛事View
    UIView *compeVC = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 300 - 49)];
    compeVC.backgroundColor = [UIColor grayColor];
    [self.competitionVC addSubview:compeVC];
    [compeVC release];
    //相关View
    UIView *conVC = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height - 300 - 49)];
    conVC.backgroundColor = [UIColor greenColor];
    [self.competitionVC addSubview:conVC];
    [conVC release];
    
    //赛事上得tableView
    
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height  - 49 - 300) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor grayColor];
        [compeVC addSubview:_tableView];
        [_tableView release];
    
        [self.tableView registerClass:[DetailsTableViewCell class] forCellReuseIdentifier:@"saishi"];
    
    //相关上得tableView
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height  - 49 - 300) style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.backgroundColor = [UIColor grayColor];
    [conVC addSubview:_tableV];
    [_tableV release];
    
    [self.tableV registerClass:[XiangguanTableViewCell class] forCellReuseIdentifier:@"xiangguan"];
    
    

}

////-(void)creatableview
//{
//    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//    self.tableView.delegate = self;
//    self.tableView.dataSource = self;
//    [self.view addSubview:_tableView];
//    [_tableView release];
//
//    [self.tableView registerClass:[DetailsTableViewCell class] forCellReuseIdentifier:@"cell5"];
//
//}


//赛事TB网络请求
-(void)saishiqingqiu
{
    NSString *str1 =@"http://qt.qq.com/php_cgi/lol_mobile/club/varcache_gamelist.php?id=";
    NSString *str2 = @"&plat=ios&version=3&page=0&num=10";
    NSString *str = [NSString stringWithFormat:@"%@%@%@",str1,_detailsid,str2];
    [AFNModel GETWithURL:str completion:^(id result) {
        NSArray *arr = result[@"matchlist"];
        NSLog(@"%@",arr);
        self.sArr = [SaishiModel modelHanderWithArray:arr];
        [_tableView reloadData];
    }];
}

//tableView的两个方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableView) {
        return _sArr.count;

    }else{
        return _xArr.count;
    }
}

//相关TB网络请求
-(void)xiangguan
{
    NSString *str1 = @"http://qt.qq.com/php_cgi/lol_mobile/club/varcache_team_about.php?channel=news&clubid=";
    NSString *str2 = @"&page=1&num=20&plat=ios&version=3";
    NSString *str = [NSString stringWithFormat:@"%@%@%@",str1,_detailsid,str2];
    [AFNModel GETWithURL:str completion:^(id result) {
        NSArray *arr = result[@"list"];
        self.xArr = [XiangguanModel modelHanderWithArray:arr];
        [_tableV reloadData];
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //判断tableView加在哪个View上
    if (tableView == _tableView) {
        DetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"saishi" forIndexPath:indexPath];
        SaishiModel *m = _sArr[indexPath.row];
        [cell.guankan addTarget:self action:@selector(guankan:) forControlEvents:UIControlEventTouchUpInside];
        cell.time.text = m.matchtime;
        cell.saishi.text = m.name;
        cell.teamaname.text = m.teamaname;
        cell.teambname.text = m.teambname;
        if ([m.matchresult isEqualToString:@""]) {
            cell.bifen.text = @"0:0";
        }else{
            cell.bifen.text = m.matchresult;
        }
        [cell.teamalogo sd_setImageWithURL:[NSURL URLWithString:m.teamalogo]];
        [cell.teamblogo sd_setImageWithURL:[NSURL URLWithString:m.teamblogo]];
        return cell;
    }else{
        XiangguanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xiangguan" forIndexPath:indexPath];
        XiangguanModel *s = _xArr[indexPath.row];
        cell.title.text = s.title;
        cell.neirong.text = s.summary;
        [cell.img sd_setImageWithURL:[NSURL URLWithString:s.image_url_small]];
        return cell;
        
    }
    

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _tableView) {
        SaishiXQViewController *saishiVC = [[SaishiXQViewController alloc] init];
        SaishiModel *m = _sArr[indexPath.row];
        saishiVC.wangzhi = m.refWebUrl;
        NSLog(@"%@",m.refWebUrl);
        [self.navigationController pushViewController:saishiVC animated:YES];
    }else{
        XiangguanXQViewController *xiangguanVC = [[XiangguanXQViewController alloc] init];
        XiangguanModel *m = _xArr[indexPath.row];
        xiangguanVC.wangzhi = m.article_url;
        [self.navigationController pushViewController:xiangguanVC animated:YES];
    }
    

}

//赛事button点击
- (void)guankan:(UIButton *)btn
{
    }


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}



//网络请求
-(void)data
{
    NSString *str1 = @"http://qt.qq.com/php_cgi/lol_mobile/club/varcache_team_simple.php?id=";
    NSString *str2 = @"&membernum=10&plat=ios&version=801";
    NSString *str = [NSString stringWithFormat:@"%@%@%@",str1,_detailsid,str2];
    [AFNModel GETWithURL:str completion:^(id result) {
        self.dic = result;
        NSArray *arr = result[@"members"];
//        self.dataArr = result[@"members"];
        self.dataArr = [TeamModel modelHanderWithArray:arr];
        [self.tableView reloadData];
//        NSLog(@"%@",_dataArr[1]);
        [self wangluo];
        
        [self shuzu];
        
        
        
    }];
}


//头部
-(void)head
{
    self.team = [[UIImageView alloc] init];
    _team.backgroundColor = [UIColor redColor];
    [self.view addSubview:_team];
    [_team release];
    
    self.name = [[UILabel alloc] init];
    _name.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_name];
    [_name release];
    
    self.fans = [[UILabel alloc] init];
    _fans.backgroundColor = [UIColor redColor];
    [self.view addSubview:_fans];
    [_fans release];
    
    self.zhanduiziliao = [UIButton buttonWithType:UIButtonTypeCustom];
    _zhanduiziliao.backgroundColor = [UIColor blackColor];
    [_zhanduiziliao setTitle:@"战队资料" forState:UIControlStateNormal];
    [_zhanduiziliao addTarget:self action:@selector(zhandui:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_zhanduiziliao];
    
    self.guanzhu = [UIButton buttonWithType:UIButtonTypeCustom];
    _guanzhu.backgroundColor = [UIColor blackColor];
    [_guanzhu setTitle:@"+关注" forState:UIControlStateNormal];
    [self.view addSubview:_guanzhu];
    
    self.fensi = [[UILabel alloc]init];
    _fensi.backgroundColor = [UIColor yellowColor];
    _fensi.text  = @"粉丝:";
    [self.view addSubview:_fensi];
    [_fensi release];
    
    _team.frame = CGRectMake(10, 74, 70, 70);
    _name.frame = CGRectMake(80, 74, 60, 30);
    _fans.frame = CGRectMake(280, 74, 100, 30);
    _zhanduiziliao.frame = CGRectMake(280, 114, 80, 20);
    _guanzhu.frame = CGRectMake(80, 104, 60, 30);
    _fensi.frame = CGRectMake(240, 74, 40, 30);
    
    

}

//进入战队资料点击
-(void)zhandui:(UIButton *)btn
{
    ZhanDuiViewController *zdVC = [[ZhanDuiViewController alloc] init];
    zdVC.detailsid = _detailsid;
    NSLog(@"%@",_detailsid);
    [self.navigationController pushViewController:zdVC animated:YES];
}

//头部网络请求
-(void)wangluo
{
    if (self.dic != nil) {
        _name.text = [self.dic objectForKey:@"name"];
        [_team sd_setImageWithURL:[NSURL URLWithString:_dic[@"iconUrl"]]];
        _fans.text = [[self.dic objectForKey:@"fansCount"] stringValue];
    }
    
}

//scrollview

-(void)createscroll
{
    //创建
    self.scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, 100)];
//    _scrollview.backgroundColor = [UIColor grayColor];
    //内容大小
    self.scrollview.contentSize= CGSizeMake(self.view.frame.size.width *1.5, 100);
//    [_scrollview release];
    
    [self.view addSubview:_scrollview];
    
    
    //开启交互
    _scrollview.userInteractionEnabled = YES;
    
    //轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    tap.numberOfTouchesRequired = 1;
    tap.numberOfTapsRequired = 1;
    [_scrollview addGestureRecognizer:tap];
    [tap release];




}

//轻拍手势实现
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    ZhanDuiViewController *zdVC = [[ZhanDuiViewController alloc] init];
    zdVC.detailsid = _detailsid;
    NSLog(@"%@",_detailsid);
    [self.navigationController pushViewController:zdVC animated:YES];
}



//头像条网络请求
-(void)shuzu
{
    //处理图片数组
    for (NSInteger i = 0; i < _dataArr.count; i++) {
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(i * 70, 20, 60, 60)];
//        TeamModel *m = [[TeamModel alloc] init];
        [img sd_setImageWithURL:[NSURL URLWithString:[_dataArr[i] iconUrl]]];
        img.backgroundColor = [UIColor redColor];
        [_scrollview addSubview:img];
}
}





//赛程相关BUTTON
//-(void)button
//{
//    self.competitionbtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    _competitionbtn.backgroundColor = [UIColor grayColor];
//    [_competitionbtn setTitle:@"赛事" forState:UIControlStateNormal];
//    _competitionbtn.frame = CGRectMake(0, 250, self.view.frame.size.width / 2, 40);
//    [self.view addSubview:_competitionbtn];
//    [_competitionbtn release];
//    
//    self.concernbtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    _concernbtn.backgroundColor = [UIColor redColor];
//    [_concernbtn setTitle:@"相关" forState:UIControlStateNormal];
//    _concernbtn.frame = CGRectMake(self.view.frame.size.width / 2, 250, self.view.frame.size.width / 2, 40);
//    [self.view addSubview:_concernbtn];
//    [_concernbtn release];
//    
//    
//}


//-(void)creatableview
//{
//    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
